package com.cn.jm.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.cloudauth.model.v20190307.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.cn.jm.core.utils.util.NoUtils;
import com.google.gson.Gson;
import com.jfinal.kit.StrKit;

/**
 * @author
 * @description 阿里云实名认证获取VerifyToken
 * @date 2020/3/16
 */
public class DescribeVerifyTokenUtil {

    private static final String TYPE = "RPBasic"; //认证方案
    private static final String BizType = "basic";


    private static final String ACCESSKEYID = "";
    private static final String ACCESSKEYSECRET = "";


    /**
     * 功能描述: 获取VerifyToken
     * @author
     * @date 2020/3/16
     * @param
     * @return java.lang.String
     */
    public static String getVerifyToken(String bizId) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESSKEYID, ACCESSKEYSECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        DescribeVerifyTokenRequest request = new DescribeVerifyTokenRequest();
        request.setRegionId("cn-hangzhou");
        request.setBizType(BizType);
        request.setBizId(bizId);

        try {
            DescribeVerifyTokenResponse response = client.getAcsResponse(request);
            String result = new Gson().toJson(response);
            JSONObject jsonObject = JSONObject.parseObject(result);
            return jsonObject.getString("verifyToken");
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 功能描述: 认证
     * @author
     * @date 2020/3/19
     * @param userName
     * @param idcardNo
     * @return int（1：通过；2-n：不通过）
     */
    public static int VerifyMaterial(String userName, String idcardNo) {
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",    // 固定cn-hangzhou
                "AccessKey",      // 您的Access Key ID
                "AccessSecret");  // 您的Access Key Secret
        IAcsClient client = new DefaultAcsClient(profile);

        //若接入方的人脸图片是本地资源，则可以使用实人认证提供的上传SDK将图片直传到实人认证OSS Bucket并获取到图片地址。如果您的业务需要使用上传SDK，请提交工单联系我们获取。
//        CloudAuthClientUploader uploader = CloudAuthClientUploader.getClentUploader(client); // 获取上传oss的实例
//        String faceImageUrl = uploader.uploadBase64("待上传的base64图片资源"); // 上传oss并获取图片链接

        //接入方服务端提交认证，接口文档：https://help.aliyun.com/document_detail/127472.html
        VerifyMaterialRequest request = new VerifyMaterialRequest();
        request.setRegionId("cn-hangzhou");
//        request.setSysProtocol(ProtocolType.HTTPS);

        request.setBizId("39ecf51e-2f81-4dc5-90ee-ff86125be683");
        request.setBizType(BizType); //创建方法请参见https://help.aliyun.com/document_detail/127885.html

//        request.setFaceImageUrl(faceImageUrl); // faceImageUrl可以是通过直传OSS获取到的链接，也可以是接入方公网可访问的人脸图片链接，支持http/https
        request.setName(userName);
        request.setIdCardNumber(idcardNo);

        VerifyMaterialResponse verifyMaterialResponse = null;
        try {
            verifyMaterialResponse = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        int statusCode = verifyMaterialResponse.getVerifyStatus(); //同步返回认证状态和相应材料
        return statusCode;
    }

    /**
     * 功能描述: 获取认证资料
     * @author
     * @date 2020/3/19
     * @return
     */
    public static DescribeVerifyResultResponse verifyResult(String bizId){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESSKEYID, ACCESSKEYSECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        //2. 接入方服务端将token传递给接入方无线客户端
        //3. 接入方无线客户端用token调起无线认证SDK
        //4. 用户按照由无线认证SDK组织的认证流程页面的指引，提交认证资料
        //5. 认证流程结束退出无线认证SDK，进入客户端回调函数
        //6. 接入方服务端获取认证状态和认证资料（注：客户端无线认证SDK回调中也会携带认证状态, 但建议以服务端调接口获取的为准进行业务上的判断和处理）
        //RPBasic、RPManual方案查询认证结果接口文档：https://help.aliyun.com/document_detail/127469.html
        //FDBioOnly方案查询认证结果接口文档：https://help.aliyun.com/document_detail/127731.html
        DescribeVerifyResultRequest verifyResultRequest = new DescribeVerifyResultRequest();
        verifyResultRequest.setRegionId("cn-hangzhou");
        verifyResultRequest.setSysProtocol(ProtocolType.HTTPS);

        verifyResultRequest.setBizId(bizId);
        verifyResultRequest.setBizType(BizType);
        try {
            DescribeVerifyResultResponse verifyResultResponse = client.getAcsResponse(verifyResultRequest);
            return verifyResultResponse;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
